package com.diy.sigmund.diycommon.spring.extensionpoint;

import java.lang.reflect.Constructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author ylm-sigmund
 * @since 2022/9/20 17:13
 */
@Component
public class CustomSmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {

    private static final Logger log = LoggerFactory.getLogger(CustomSmartInstantiationAwareBeanPostProcessor.class);

    @Override
    public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
        return SmartInstantiationAwareBeanPostProcessor.super.predictBeanType(beanClass, beanName);
    }

    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        return SmartInstantiationAwareBeanPostProcessor.super.determineCandidateConstructors(beanClass, beanName);
    }

    /**
     * 循环依赖
     * <br>Line 330: 2022-09-20 17:20:47.979 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomSmartInstantiationAwareBeanPostProcessor] - 循环依赖:bean = HikariDataSource (null), beanName = dataSource
     * <br>Line 715: 2022-09-20 17:20:52.415 [diy-common] [172.25.96.1] [INFO ] [main] [com.diy.sigmund.diycommon.spring.extensionpoint.CustomSmartInstantiationAwareBeanPostProcessor] - 循环依赖:bean = org.springframework.web.servlet.view.ContentNegotiatingViewResolver@6e80de85, beanName = viewResolver
     */
    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        log.info("循环依赖:bean = " + bean + ", beanName = " + beanName);
        return SmartInstantiationAwareBeanPostProcessor.super.getEarlyBeanReference(bean, beanName);
    }
}
